package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallMechanismUserMapper;
import org.linlinjava.litemall.db.domain.LitemallMechanismUser;
import org.linlinjava.litemall.db.domain.LitemallMechanismUserExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class LitemallMechanismUserService {
    @Resource
    private LitemallMechanismUserMapper mechanismUserMapper;

    public LitemallMechanismUser findById(Integer userId) {
        return mechanismUserMapper.selectByPrimaryKey(userId);
    }
    public LitemallMechanismUser findUserByMid(Integer mid){
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andMechanismIdEqualTo(mid).andDeletedEqualTo(false);
        return mechanismUserMapper.selectOneByExample(example);
    }
    public void add(LitemallMechanismUser user) {
        mechanismUserMapper.insertSelective(user);
    }

    public void update(LitemallMechanismUser user) {
        mechanismUserMapper.updateByPrimaryKeySelective(user);
    }

    public List<LitemallMechanismUser> querySelective(Integer mechanismId,String username, String fullNamme, Integer page, Integer size, String sort, String order) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        LitemallMechanismUserExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(fullNamme)){
            criteria.andFullNameLike("%" + fullNamme + "%");
        }
        if(!StringUtils.isEmpty(mechanismId)){
            criteria.andMechanismIdEqualTo(mechanismId);
        }
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(page, size);
        return mechanismUserMapper.selectByExample(example);
    }

    public int countSeletive(Integer memanismId, String username, String fullname, Integer page, Integer size, String sort, String order) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        LitemallMechanismUserExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(fullname)){
            criteria.andFullNameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(memanismId)){
            criteria.andMechanismIdEqualTo(memanismId);
        }
        criteria.andDeletedEqualTo(false);

        return (int) mechanismUserMapper.countByExample(example);
    }

    public int countSeletive(String status, Integer id, String username) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        LitemallMechanismUserExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        if (status != null && !StringUtils.isEmpty(status) && status.equals("update")) {
            criteria.andIdNotEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);

        return (int) mechanismUserMapper.countByExample(example);
    }
    public int count() {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andDeletedEqualTo(false);

        return (int)mechanismUserMapper.countByExample(example);
    }

    public List<LitemallMechanismUser> queryByUsername(String username) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return mechanismUserMapper.selectByExample(example);
    }

    public List<LitemallMechanismUser> queryByMobile(String fullname) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andFullNameEqualTo(fullname).andDeletedEqualTo(false);
        return mechanismUserMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        mechanismUserMapper.logicalDeleteByPrimaryKey(id);
    }

    public void deleteByMechanismId(Integer id) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andMechanismIdEqualTo(id);
        mechanismUserMapper.logicalDeleteByExample(example);
    }

    public void updateBalanceByMechanismId(BigDecimal balance, Integer mechanismId){
        mechanismUserMapper.updateBalanceByMechanismId(balance, mechanismId);
    }

    public void updateBalance(BigDecimal balance, Integer mechanismId){
        mechanismUserMapper.updateBalance(balance, mechanismId);
    }

    public LitemallMechanismUser queryByMechanismId(Integer mechanismId){
        return mechanismUserMapper.queryByMechanismId(mechanismId);
    }

    public LitemallMechanismUser queryByMechanismsId(Integer mechanismId){
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andDeletedEqualTo(false).andMechanismIdEqualTo(mechanismId);
        return mechanismUserMapper.selectOneByExample(example);
    }

    public int num(Integer mechanismId) {
        LitemallMechanismUserExample example = new LitemallMechanismUserExample();
        example.or().andMechanismIdEqualTo(mechanismId);
        return (int)mechanismUserMapper.countByExample(example);
    }

    public LitemallMechanismUser queryByOpenId(String openid){
        return mechanismUserMapper.queryByOpenId(openid);
    }
    public int  findMechanismIdByUserId (Integer userId){return mechanismUserMapper.queryMechanismIdByUserId(userId);}
}
